<template>
  <a-modal
    :visible="show"
    width="80vw"
    min-height="60vh"
    title="科技文献管理"
    @ok="handleOk"
    @cancel="handleCancle"
    :maskClosable="false"
    :key="show"
  >
    <div class="header" style="padding: 10px 0px">
      <a-row type="flex" justify="space-between" align="middle">
        <a-col :xl="{ span: 14 }" :lg="{ span: 11 }">
          <a-button class="hte-btn add-btn" type="primary" @click="showTips">新建</a-button>
          <!-- <a-button class="hte-btn" plain @click="handleExport">导出</a-button>
          <a-button class="hte-btn" plain @click="handleReload">刷新</a-button>
          <a-button class="hte-btn" plain @click="handleDel" style="color: red">删除</a-button> -->
          <a-button class="hte-btn add-btn" type="plain" style="margin-left: 15px" @click="handleNotice">提醒</a-button>

          <a-button class="hte-btn add-btn" type="plain" style="margin-left: 15px" @click="fetchList">刷新</a-button>

          <span style="margin-left: 15px; color: red">
            <span
              v-for="(record, index) in codes['T类科技项目文献数量要求']"
              :key="index"
              v-show="projectLevel == projectType + record.code"
              :style="parseInt(record.additionalFieldThree) <= monthCount ? 'color:green' : ''"
            >
              项目级别：{{ projectLevel }}; 项目组月度应产出文献【{{ record.additionalFieldThree }}】篇，本月已产出【{{
                monthCount
              }}】篇。
            </span>
          </span>
        </a-col>
        <!-- <a-col :xl="{ span: 9 }" :lg="{ span: 13 }" class="input-ground">
          <div style="display: flex">
            <a-input-group compact>
              <div style="display: flex">
                <a-select v-model="keywordType" @change="changeKeywordType">
                  <a-select-option value="1"> 变更单号</a-select-option>
                  <a-select-option value="2"> 项目编号 </a-select-option> -
                </a-select>
                <a-input-search
                  v-if="keywordType == '1'"
                  v-model="searchForm.projectChangeNo"
                  @search="handleToSearch"
                  placeholder="请按照关键字进行搜索"
                />
                <a-input-search
                  v-if="keywordType == '2'"
                  v-model="searchForm.projectNo"
                  @search="handleToSearch"
                  placeholder="请按照关键字进行搜索"
                />
              </div>
            </a-input-group>
            <a-button class="hte-btn del-btn" @click="showSearchForm = true">高级搜索</a-button>
          </div>
        </a-col> -->
      </a-row>
      <!-- <a-modal
        :visible="showSearchForm"
        width="70vw"
        :bodyStyle="{ padding: '10px' }"
        title="高级查询"
        @ok="handleToSearch"
        @cancel="showSearchForm = false"
      >
        <a-form-model
          ref="ruleForm"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          @keyup.enter.native="handleToSearch"
        >
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="审批状态">
                <a-select v-model="searchForm.withInStatus" style="width: 100%" allowClear>
                  <a-select-option value=""> 全部 </a-select-option>
                  <a-select-option v-for="(code, index) in codes['工程项目变更状态']" :key="index" :value="code.code">
                    {{ code.name }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="变更单号">
                <a-input allow-clear v-model="searchForm.projectChangeNo" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="变更名称">
                <a-input allow-clear v-model="searchForm.projectChangeName" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="主要负责人">
                <a-input allow-clear v-model="searchForm.dutyUserName" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal> -->
    </div>
    <div class="content-list">
      <a-table
        :columns="columns"
        :data-source="records"
        rowKey="id"
        bordered
        :pagination="false"
        :customRow="customRow"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :loading="loading"
      >
      </a-table>
      <a-pagination
        show-size-changer
        :default-current="1"
        :total="searchForm.total"
        @change="pageChange"
        @showSizeChange="onShowSizeChange"
        :show-total="(total) => `共 ${total} 条记录`"
      />
    </div>
    <HteFilePopTips ref="HteFilePopTips" @ok="handleAdd" />
  </a-modal>
</template>

<script>
import { getAction, postAction, downFile, downloadFileLink } from '@/api/manage'
import { TableMixin } from '@/mixins/TableMixin'
import { CodesMixin } from '@/mixins/CodesMixin'
import { formatDate, parseTime } from '@/utils/util'
export default {
  name: 'HteFilePopList',
  components: {
    HteFilePopTips: () => import('@/views/library/HteFilePopTips'),
  },
  mixins: [TableMixin, CodesMixin],
  data() {
    return {
      show: false,
      loading: false,
      urls: {
        page: '/hteProjectLibFile/page',
        currentMonthPage: '/hteProjectLibFile/currentMonthPage',
        getTreeList: '/lib/hteFileClass/getTreeList',
        sendNotice: '/hteProjectLibFile/sendNotice',
      },
      columns: [
        {
          title: '序号',
          dataIndex: 'sortIndex',
          width: '80px',
          align: 'center',
          customRender: (text, record, index) => {
            return index + 1
          },
        },
        {
          title: '文件类别',
          dataIndex: 'fileType',
          width: '100px',
          align: 'center',
          customCell: () => {
            return this.customCellRender(130)
          },
          customRender: (text, record) => {
            return this.tooltipRender(this.checkFileTypeTitle(text), record)
          },
        },
        {
          title: '文件名称',
          dataIndex: 'fileName',
          align: 'center',
          width: '130px',
          customCell: () => {
            return this.customCellRender(130)
          },
          customRender: (text, record) => {
            return this.tooltipActiveRender(text, record, 'openLibFile')
          },
        },
        {
          title: '文件编号',
          dataIndex: 'fileNo',
          align: 'left',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipActiveRender(text, record, 'openLibFile')
          },
        },
        {
          title: '发布部门',
          dataIndex: 'publishDept',
          align: 'center',
          width: '230px',
        },
        {
          title: '发布人',
          dataIndex: 'publishUser',
          align: 'center',
          width: '230px',
        },
        {
          title: '发布生效时间',
          dataIndex: 'effectTime',
          align: 'center',
          width: '140px',
          customCell: () => {
            return this.customCellRender(100)
          },
          customRender: (text, record) => {
            return this.timeRender(text, record, 'yyyy-MM-dd')
          },
        },
        {
          title: '申请时间',
          dataIndex: 'libCreateTime',
          align: 'center',
          width: '100px',
          customCell: () => {
            return this.customCellRender(100)
          },
          customRender: (text, record) => {
            return this.timeRender(text, record, 'yyyy-MM-dd')
          },
        },
      ],
      treeData: [],
      monthList: [],
      monthCount: 0,
      types: ['T类科技项目文献数量要求'],
      projectLevel: '',
      projectType: '',
      projectBuildId: '-1',
    }
  },
  created() {
    this.fetchClassTree()
    this.fetchCodes(this.types)
  },
  methods: {
    openInit(projectBuildId, projectNo, projectType, projectLevel) {
      this.records = []
      this.projectBuildId = projectBuildId
      this.projectLevel = projectLevel
      this.projectType = projectType
      this.$set(this.searchForm, 'projectNo', projectNo)
      this.fetchList()
      this.fetchMonthList()
      this.show = true
    },
    fetchList() {
      this.loading = true
      this.records = []
      postAction(this.urls.page, this.searchForm).then((res) => {
        if (res.success) {
          this.records = res.result.records
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('查询数据错误:' + res.message)
        }
        this.loading = false
      })
    },
    fetchMonthList() {
      this.loading = true
      this.records = []
      postAction(this.urls.currentMonthPage, { pageNo: 1, pageSize: 100, projectNo: this.searchForm.projectNo }).then(
        (res) => {
          if (res.success) {
            this.monthList = res.result.records
            this.monthCount = res.result.total
          } else {
            this.$message.error('查询数据错误:' + res.message)
          }
          this.loading = false
        }
      )
    },
    handleOk() {
      this.handleCancle()
    },
    handleCancle() {
      this.show = false
    },
    showTips() {
      this.$refs.HteFilePopTips.openInit()
    },
    handleAdd() {
      window.open(process.env.BASE_URL+'library/file/new/4?projectNo=' + this.searchForm.projectNo, '_blank')
    },
    openLibFile(projectLibFile) {
      window.open(
        process.env.BASE_URL+'library/file/view/4/' +
          projectLibFile.libFileId +
          '/' +
          projectLibFile.draftVersion +
          '?projectNo=' +
          projectLibFile.projectNo
      )
    },
    /**
     * 获取分类树
     */
    fetchClassTree() {
      getAction(this.urls.getTreeList, { parentId: 4 }).then((res) => {
        if (res.success) {
          if (this.parentId == 2) {
            this.selectableTreeData(res.result.treeList)
          }
          this.treeData = res.result.treeList
        } else {
          this.$message.error('获取分类树错误')
        }
      })
    },
    checkFileTypeTitle(text) {
      let currentFileType = this.getFileType(this.treeData, text)
      if (currentFileType) {
        return currentFileType.title
      } else {
        return ''
      }
    },
    getFileType(list, text) {
      for (let l of list) {
        if (l.value == text) {
          return l
        } else {
          if (l.children && l.children.length > 0) {
            let cl = this.getFileType(l.children, text)
            if(cl){
              return cl
            }
          }
        }
      }
    },
    handleNotice() {
      getAction(this.urls.sendNotice, { projectBuildId: this.projectBuildId }).then((res) => {
        if (res.success) {
          this.$message.success('提醒成功')
        } else {
          this.$message.error('提醒失败')
        }
      })
    },
  },
}
</script>

<style lang="less" scoped></style>
